package com.lqfan.autoAnswerQ.controller.reportCard;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lqfan.autoAnswerQ.controller.errorRecords.ErrorRecordsController;
import com.lqfan.autoAnswerQ.model.reportCard.ReportCard;
import com.lqfan.autoAnswerQ.service.reportCard.IReportCardService;
import com.lqfan.autoAnswerQ.utils.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Options;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zml
 * @since 2020-05-04
 */
@RestController
@RequestMapping("/reportCard")
@Api(value = "成绩单")
public class ReportCardController {
    @Autowired
    IReportCardService reportCardService;
    private static final Logger logger = LoggerFactory.getLogger(ErrorRecordsController.class);

    @ApiOperation(value = "插入信息到成绩单", notes = "插入信息到成绩单")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "questionId", value = "题库id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "score", value = "分数", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "info", value = "信息", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "name", value = "题库名称", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String")
            }
    )
    @RequestMapping(value = "addRecordCard", method = RequestMethod.POST)
    public ResponseMessage insertRecordCard(@RequestParam(value = "questionId") String questionId,
                                            @RequestParam(value = "score") String score,
                                            @RequestParam(value = "info") String info,
                                            @RequestParam(value = "name") String name,
                                            @RequestParam(value = "userId") String userId) {
        logger.info("插入信息到成绩单");
        int recordsList = reportCardService.insertRecordCard(questionId,score,info,name,userId);
        switch (recordsList){
            case 0:
                return ResponseMessage.fail("插入成绩单失败");
            case 1:
                return ResponseMessage.success("插入成绩单成功");
        }
        return ResponseMessage.fail("插入成绩单失败");
    }


    @ApiOperation(value = "获取单个题库的所有成绩单", notes = "获取单个题库的所有成绩单")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "questionId", value = "题库id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String")
            }
    )
    @RequestMapping(value = "getCardInfo", method = RequestMethod.POST)
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public ResponseMessage getCardInfo(@RequestParam(value = "questionId") String questionId,
                                            @RequestParam(value = "userId") String userId) {
        logger.info("获取单个题库的所有成绩单");
        List<ReportCard> recordsList = reportCardService.getReportCardInfo(userId,questionId);
        if (recordsList==null){
            return ResponseMessage.fail("获取数据失败");
        }else {
            return ResponseMessage.success(recordsList);
        }
    }

    @ApiOperation(value = "获取所有成绩单相关的题库", notes = "获取所有成绩单相关的题库")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String")
            }
    )
    @RequestMapping(value = "getCard", method = RequestMethod.POST)
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public ResponseMessage getCard(@RequestParam(value = "userId") String userId) {
        logger.info("获取所有成绩单相关的题库");
        List<ReportCard> recordsList = reportCardService.getReportCard(userId);
        if (recordsList==null){
            return ResponseMessage.fail("获取数据失败");
        }else {
            return ResponseMessage.success(recordsList);
        }
    }


    @ApiOperation(value = "删除成绩单对应的题库", notes = "删除成绩单对应的题库")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "questionId", value = "题库id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String")
            }
    )
    @RequestMapping(value = "deleteRecordCard", method = RequestMethod.POST)
    public ResponseMessage deleteRecordCard(@RequestParam(value = "questionId") String questionId,
                                            @RequestParam(value = "userId") String userId) {
        logger.info("删除成绩单对应的题库");
        int recordsList = reportCardService.deleteReportCard(questionId,userId);
        switch (recordsList){
            case 0:
                return ResponseMessage.fail("删除失败");
            case 1:
                return ResponseMessage.success("删除成功");
        }
        return ResponseMessage.fail("删除失败");
    }

    @ApiOperation(value = "删除题库对应的成绩单", notes = "删除题库对应的成绩单")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "reportCardId", value = "题库中成绩单的id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String")
            }
    )
    @RequestMapping(value = "deleteRecordCardInfo", method = RequestMethod.POST)
    public ResponseMessage deleteRecordCardInfo(@RequestParam(value = "reportCardId") String reportCardId,
                                            @RequestParam(value = "userId") String userId) {
        logger.info("删除题库对应的成绩单");
        int recordsList = reportCardService.deleteReportCardInfo(reportCardId,userId);
        switch (recordsList){
            case 0:
                return ResponseMessage.fail("删除失败");
            case 1:
                return ResponseMessage.success("删除成功");
        }
        return ResponseMessage.fail("删除失败");
    }
}

